package com.tzinfo.tzb;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tzinfo.tzb.util.SharedPreferencesTools;
import com.tzinfo.tzt.R;
import com.yxw.util.OutLog;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.util.ResFinder;

public class TzbBaseActivity extends Activity {

	public boolean isExit = false;
	protected ProgressBar mProgressBar;
	protected DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected SharedPreferencesTools sharedPreferencesTools;
	protected Dialog dialog;

	@SuppressWarnings("rawtypes")
	public void LoadActivity(Class ClassName, boolean needFinish) {
		startActivity(new Intent(TzbBaseActivity.this, ClassName));
		overridePendingTransition(R.anim.left_in,R.anim.left_out);
		if (needFinish) {
			finish();
		}
	}

	@Override
	public void finish() {
		super.finish();
	}

	protected void removeProgressBar() {
		if (mProgressBar != null) {
			mProgressBar.setVisibility(View.GONE);
		}
	}

	protected void showProgressBar() {
		if (mProgressBar != null) {
			mProgressBar.setVisibility(View.VISIBLE);
		}
	}

	public void LoadActivityWithTabhost(Intent intent, ActivityGroup group) {
		LinearLayout container = (LinearLayout) group.getWindow().findViewById(
				ResFinder.getR(this, "R.id.container"));
		container.removeAllViews();
		container.addView(group
				.getLocalActivityManager()
				.startActivity("PagerView",
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView());

		// intent.setClass(TzbBaseActivity.this, ClassName);
		// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// Window subActivity = ((ActivityGroup)
		// TzbBaseActivity.this.getParent())
		// .getLocalActivityManager().startActivity(ClassName.toString(),
		// intent);
		// // subActivity.setContentView(subActivity.getDecorView());
		// container.addView(subActivity.getDecorView());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dialog = new YXWProgressDialog(this).createDialog("加载中…");
		imageLoader.init(ImageLoaderConfiguration.createDefault(this));
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(ResFinder.getR(this, "R.drawable.ic_empty"))
				.showImageForEmptyUri(
						ResFinder.getR(this, "R.drawable.ic_empty"))
				.showImageOnFail(ResFinder.getR(this, "R.drawable.ic_empty"))
				.cacheInMemory(true).cacheOnDisc(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		sharedPreferencesTools=new SharedPreferencesTools(this);
		OutLog.LogE("yxw", "create");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		OutLog.LogE("my", "start");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isExit = false;
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		isExit = true;
	}

	// public void FinishActivity() {
	// finish();
	// overridePendingTransition(ResFinder.getR(this, "R.anim.right_in"),
	// ResFinder.getR(this, "R.anim.right_out"));
	// }

	public void goback(View view) {
		// FinishActivity();
		finish();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// FinishActivity();
			finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public Object getSystemService(String name) {
		// TODO Auto-generated method stub
		return super.getSystemService(name);
	}

	public void goTabHostBack(View v) {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 获取当前活动的Activity实例
		// 判断是否实现返回值接口
		if (this instanceof OnTabActivityResultListener) {
			// 获取返回值接口实例
			OnTabActivityResultListener listener = (OnTabActivityResultListener) this;
			// 转发请求到子Activity
			listener.onTabActivityResult(requestCode, resultCode, data);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public interface OnTabActivityResultListener {
		public void onTabActivityResult(int requestCode, int resultCode,
                                        Intent data);
	}
}